﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;

namespace RISMOTAXDP.EmailService
{
    /// <summary>
    /// 系统信息类
    /// </summary>
    public class SystemInfo
    {
        #region NewsletterMail相关

        /// <summary>
        /// NewsletterMail 检查间隔时间(分钟)
        /// </summary>
        private int m_NewsletterMailCheckInterval;

        #endregion

        #region 电子邮件相关信息
        /// <summary>
        /// 邮件生成间隔
        /// </summary>
        private int m_MailCreateInterval;
        /// <summary>
        /// 邮件检查间隔
        /// </summary>
        private int m_MailCheckInterval;
        /// <summary>
        /// 邮件收发间隔
        /// </summary>
        private int m_MailTransferInterval;
        /// <summary>
        /// 邮件发送限制
        /// </summary>
        private int m_MailTransferLimit;
        /// <summary>
        /// 邮箱登录重试次数
        /// </summary>
        private int m_MailLoginRetry;
        /// <summary>
        /// 邮箱登录重试等待时间
        /// </summary>
        private int m_MailLoginRetryTimeout;
        /// <summary>
        /// 邮箱操作重试次数
        /// </summary>
        private int m_MailOperationRetry;
        /// <summary>
        /// 邮箱操作重试等待时间
        /// </summary>
        private int m_MailOperationRetryTimeout;
 
        #endregion

        #region 数据库相关信息
        /// <summary>
        /// 数据库连接字符串格式
        /// </summary>
        private const string FORMAT_DBCONNECT = "Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}";
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        private string m_DBConnectString;
        #endregion


        #region 其他信息
        /// <summary>
        /// 初始化状态
        /// </summary>
        private bool m_Initialized;

        /// <summary>
        /// 输出异常消息
        /// </summary>
        private bool m_ExceptionOutput = false;
        /// <summary>
        /// 输出邮件生成信息
        /// </summary>
        private bool m_TraceMailCreate = false;

        #endregion


        #region 公共属性
        /// <summary>
        /// 获取邮件生成间隔
        /// </summary>
        public int MailCreateInterval
        {
            get { return m_MailCreateInterval; }
        }
        /// <summary>
        /// 获取邮件检查间隔
        /// </summary>
        public int MailCheckInterval
        {
            get { return m_MailCheckInterval; }
        }
        /// <summary>
        /// 获取邮件收发间隔
        /// </summary>
        public int MailTransferInterval
        {
            get { return m_MailTransferInterval; }
        }
        /// <summary>
        /// 获取邮件发送限制
        /// </summary>
        public int MailTransferLimit
        {
            get { return m_MailTransferLimit; }
        }
        /// <summary>
        /// 获取邮箱登录重试次数
        /// </summary>
        public int MailLoginRetry
        {
            get { return m_MailLoginRetry; }
        }
        /// <summary>
        /// 获取邮箱登录重试等待时间
        /// </summary>
        public int MailLoginRetryTimeout
        {
            get { return m_MailLoginRetryTimeout; }
        }
        /// <summary>
        /// 获取邮箱操作重试次数
        /// </summary>
        public int MailOperationRetry
        {
            get { return m_MailOperationRetry; }
        }
        /// <summary>
        /// 获取邮箱操作重试等待时间
        /// </summary>
        public int MailOperationRetryTimeout
        {
            get { return m_MailOperationRetryTimeout; }
        }
 
        /// <summary>
        /// 获取数据库连接字符串
        /// </summary>
        public string DBConnectString
        {
            get { return m_DBConnectString; }
        }

        /// <summary>
        /// 获取是否输出异常信息
        /// </summary>
        public bool ExceptionOutput
        {
            get { return m_ExceptionOutput; }
        }
        /// <summary>
        /// 获取输出邮件生成信息
        /// </summary>
        public bool TraceMailCreate
        {
            get { return m_TraceMailCreate; }
        }


        /// <summary>
        /// 返回系统信息初始化状态
        /// </summary>
        public bool Initialized
        {
            get { return m_Initialized; }
        }


        /// <summary>
        /// NewsletterMail 检查间隔时间(分钟)
        /// </summary>
        public int NewsletterMailCheckInterval
        {
            get { return m_NewsletterMailCheckInterval; }
        }



        #endregion

        /// <summary>
        /// 类初始化
        /// </summary>
        public SystemInfo()
        {
            m_Initialized = false;
        }

        /// <summary>
        /// 初始化系统信息
        /// </summary>
        /// <returns>返回执行是否成功</returns>
        public bool Initialize()
        {
            //读取系统配置信息
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                string dbServer = config.AppSettings.Settings["DBServer"].Value;
                string dbName = config.AppSettings.Settings["DBName"].Value;
                string dbUserName = config.AppSettings.Settings["DBUserName"].Value;
                string dbPassword = config.AppSettings.Settings["DBPassword"].Value;
                //if (dbServer == string.Empty || dbName == string.Empty || dbUserName == string.Empty || dbPassword == string.Empty) return false;
                if (dbServer == string.Empty || dbName == string.Empty || dbUserName == string.Empty) return false;

                m_MailCreateInterval = int.Parse(config.AppSettings.Settings["MailCreateInterval"].Value);
                m_MailCheckInterval = int.Parse(config.AppSettings.Settings["MailCheckInterval"].Value);
                m_MailTransferInterval = int.Parse(config.AppSettings.Settings["MailTransferInterval"].Value);
                m_MailTransferLimit = int.Parse(config.AppSettings.Settings["MailTransferLimit"].Value);
                if (m_MailTransferLimit <= 0) m_MailTransferLimit = int.MaxValue; //小于等于0认为无传输限制
                m_MailLoginRetry = int.Parse(config.AppSettings.Settings["MailLoginRetry"].Value);
                m_MailLoginRetryTimeout = int.Parse(config.AppSettings.Settings["MailLoginRetryTimeout"].Value);
                m_MailOperationRetry = int.Parse(config.AppSettings.Settings["MailOperationRetry"].Value);
                m_MailOperationRetryTimeout = int.Parse(config.AppSettings.Settings["MailOperationRetryTimeout"].Value);
 
                m_DBConnectString = string.Format(FORMAT_DBCONNECT, dbServer, dbName, dbUserName, dbPassword);


                //NewsletterMailCheckInterval 需要将分钟转换成秒
                m_NewsletterMailCheckInterval = int.Parse(config.AppSettings.Settings["NewsletterMailCheckInterval"].Value);
                m_NewsletterMailCheckInterval = m_NewsletterMailCheckInterval * 60;


                /*
                m_ReportTemplatePassword = config.AppSettings.Settings["ReportTemplatePassword"].Value;
                m_ReportSheetCount = int.Parse(config.AppSettings.Settings["ReportSheetCount"].Value);
                m_ReportRowsCount = int.Parse(config.AppSettings.Settings["ReportRowsCount"].Value);
                m_MailOPName = config.AppSettings.Settings["MailOPName"].Value.Split(',');
                m_MailOPFileName = config.AppSettings.Settings["MailOPFileName"].Value.Split(',');
                m_MailOPSendType = config.AppSettings.Settings["MailOPSendType"].Value.Split(',');
                m_ReportPageSize = config.AppSettings.Settings["ReportPageSize"].Value.Split(',');
                m_ReportName = config.AppSettings.Settings["ReportName"].Value.Split(',');
                m_ReportTemplateName = config.AppSettings.Settings["ReportTemplateName"].Value.Split(',');
                */

                if (config.AppSettings.Settings["ExceptionOutput"] != null)
                {
                    m_ExceptionOutput = config.AppSettings.Settings["ExceptionOutput"].Value == "1";
                }
                /*
                if (config.AppSettings.Settings["MailDeleteAfterRead"] != null)
                {
                    m_MailDeleteAfterRead = config.AppSettings.Settings["MailDeleteAfterRead"].Value == "1";
                }
                */

                if (config.AppSettings.Settings["TraceMailCreate"] != null)
                {
                    m_TraceMailCreate = config.AppSettings.Settings["TraceMailCreate"].Value == "1";
                }

                /*
                if (config.AppSettings.Settings["NotesMailSaveLocal"] != null)
                {
                    m_NotesMailSaveLocal = config.AppSettings.Settings["NotesMailSaveLocal"].Value == "1";
                }
                */
                m_Initialized = true;

                GlobalObject.WriteSystemLog("S00000005",null);
            }
            catch (Exception ex)
            {
                GlobalObject.WriteOperationLog("E80000001", ex);
            }

            return m_Initialized;
        }

        /// <summary>
        /// 重新初始化系统信息
        /// </summary>
        public void ReInitialize()
        {
            m_Initialized = false;
        }

    }



}
